package vlms.controller;

import java.io.IOException;
import java.rmi.RemoteException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import vlms.bean.IssuedMovie;
import vlms.bean.Movie;

import vlms.bean.Person;
import vlms.service.VLMSServiceProxy;
import wrapper.CartMovie;

/**
 * Servlet implementation class IssuedMovies
 */
public class IssuedMovies extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public IssuedMovies() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String message = null; 
		String destination = "/home.jsp";
		HttpSession session = request.getSession();
		Person oPerson = (Person) session.getAttribute("Person");
		String sPersonId = String.valueOf(oPerson.getPersonid());
		
		try
		 {
			 VLMSServiceProxy proxy = new VLMSServiceProxy();
			 IssuedMovie[] aIssuedMovie = proxy.retrieveIssuedMovieByPersonId(sPersonId);
			 
			 Movie[] aMovie = new Movie[aIssuedMovie.length];
			 
			 for(int i=0;i<aIssuedMovie.length;i++) {
				
				 aMovie[i] = proxy.retrieveMovieById(String.valueOf(aIssuedMovie[i].getMovieid()));
			 }
				 
			 
			 
			 request.setAttribute("IssuedMovie", aIssuedMovie);
			 session.setAttribute("Movie", aMovie);
			 destination = "/viewCart.jsp";
		 }
		 catch(RemoteException re)
		 {
			 destination = "/viewCart.jsp";
			 request.setAttribute("message", re.getMessage());
			 re.printStackTrace();
		 }
		
		
		
		RequestDispatcher rd = this.getServletContext().getRequestDispatcher(destination);
		rd.forward(request, response);
		
		
	}

}
